package com.huihui.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.huihui.R;
import com.huihui.activity.MainActivity;
import com.huihui.base.BaseActivity;
import com.huihui.bean.Article;
import com.huihui.bean.Goods;
import com.huihui.datamanager.AppManager;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.utils.IntentUtils;
import com.huihui.utils.LoadImageUtil;
import com.huihui.utils.ShareUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.Call;


/**
 * Created by Administrator on 2018/8/19.
 */

public class ArticleAdapter extends RecyclerView.Adapter<ItemViewHolder>{
    private Context context;
    private List<Article> list;
    private MineZanGridViewAdapter adapter;

    public ArticleAdapter(Context context, List<Article> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mine_zan, parent, false);
        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final ItemViewHolder holder,  int position) {
        final Article article = list.get(position);
        if(article != null) {
            //设置数据
            LoadImageUtil.displayImage(context, article.getHead_img(), holder.head_view);
            holder.user_name.setText(article.getUser_nick_name());
            holder.user_time.setText(article.getArt_time());
            holder.desc.setText(article.getArt_content());
            //设置adapter
            MineZanGridViewAdapter adapter = new MineZanGridViewAdapter(article.getArt_content_url(),context);
            holder.gridview.setAdapter(adapter);
            holder.zhuanfa.setText(String.valueOf(article.getShare_num()));
            holder.pinglun.setText(String.valueOf(article.getComment_num()));
            if(article.getCollection_num() > 0){
            }
            holder.zan.setText(String.valueOf(article.getCollection_num()));
            holder.item_layout.setTag(position);
            holder.item_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到详情页
                    int p = (int) v.getTag();
                    IntentUtils.gotoDetialAcitvity(context,null,article,p);
                }
            });
            holder.fenxiangLayout.setTag(position);
            holder.fenxiangLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int p = (int) v.getTag();
                    showSharePop(v,p);
                }
            });
            holder.report.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    doReport(article);
                }
            });
            holder.pinglun_layout.setTag(position);
            holder.dianzan_layout.setTag(position);
            if(onPinlunlickListener != null){
                holder.pinglun_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int p = (int) v.getTag();
                        onPinlunlickListener.onClick(p);
                    }
                });
            }
            if(onDianZanListener != null){
                holder.dianzan_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int p = (int) v.getTag();
                        onDianZanListener.onClick(p);
                    }
                });
            }
            if(0 == article.getIs_like()){
                holder.zan_icon.setImageResource(R.mipmap.zan);
            }else {
                holder.zan_icon.setImageResource(R.mipmap.zan_press);
            }
        }
    }

    /**
     * 举报
     * @param article
     */
    private void doReport(Article article) {
        IntentUtils.gotoReportActivity(context,article.getUser_nick_name(),article.getUser_id(),article.getArt_id(),3,article.getArt_content());
    }

    private void showSharePop(View layout, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.pop_share_layout,null);
        final PopupWindow pop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
        pop.setOutsideTouchable(true);
        pop.setFocusable(true);
        pop.setBackgroundDrawable(new ColorDrawable());
        pop.showAtLocation(layout, Gravity.BOTTOM| Gravity.CENTER_HORIZONTAL,0,0);
        backgroundAlpha(0.5f);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        view.findViewById(R.id.wechat_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send(pop,position,0);
            }
        });

        view.findViewById(R.id.wechat_cicle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send(pop,position,1);
            }
        });
        view.findViewById(R.id.pop_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
    }

    private void send(final PopupWindow pop, final int position, final int type){
        pop.dismiss();
        //获取链接
        OkHttpRequest.getInstance().getShareUrl(HttpContants.SHARE_URL, AppManager.getUserInfo().getUser_id(), 2, list.get(position).getArt_id(), new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if(jsonObject != null){
                        String url = jsonObject.getString("url");
                        ShareUtils.shareSelect((Activity) context,url,type,list.get(position).getArt_content(),"信息来自讨论," + list.get(position).getUser_nick_name());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = ((BaseActivity)context).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((BaseActivity)context).getWindow().setAttributes(lp);
        ((BaseActivity)context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    public interface OnPinlunlickListener {
        void onClick(int position);
    }
    public interface onDianZanListener{
        void onClick(int position);
    }
    private MineZanAdapter.OnPinlunlickListener onPinlunlickListener;
    public void addOnPinlunlickListener(MineZanAdapter.OnPinlunlickListener listener){
        onPinlunlickListener = listener;
    }
    private MineZanAdapter.onDianZanListener onDianZanListener;
    public void addOnDianZanListener(MineZanAdapter.onDianZanListener listener){
        onDianZanListener = listener;
    }
}


